package com.naiterui.ehp.bs.doctor.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.naiterui.ehp.bp.domain.NoticeTopic;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.biz.AppJumpProtocol;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;

/**
 * IM 系统公告、动态消息 推送工具类
 *
 * @since 2.0.0
 */
public class IMnoticeUtil {

    /**
     * 日志对象：<code>logger</code>
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(IMnoticeUtil.class);

    /**
     * 公告消息单人推送接口：<code>NOTICE_SINGLE_URL</code>
     */
    private static final String NOTICE_SINGLE_URL = ConfigUtil.getString("notice.single.url");
    /**
     * 账户动态单人推送接口：<code>DYNAMIC_SINGLE_URL</code>
     */
    private static final String DYNAMIC_SINGLE_URL = ConfigUtil.getString("dynamic.single.url");

    /**
     * 系统公告单人推送(单人推送不处理push逻辑)
     *
     * @param doctorId 医生id
     * @param topic    公告主题实体
     * @param message  待推送消息
     */
    public static void singleTopicNotice(Long doctorId, NoticeTopic topic, String message) throws BusinessException {

        // 医生id校验
        if (null == doctorId || null == topic || StringUtils.isEmpty(message)) {
            LOGGER.debug("系统公告单人推送参数异常请求参数：doctorId = {}, topic = {}, message = {}", doctorId, topic, message);
            return;
        }

        String targetUrl = IMnoticeUtil.getTargetUrl(topic);
        String noticeUrl = AppJumpProtocol.DR_WEB.parse(targetUrl);
        String noticeImg = topic.getLogoImg();

        Map<String, Object> messagesParam = new HashMap<>();
        messagesParam.put("headImg", ImageUrlUtil.genNoticePath(noticeImg));
        messagesParam.put("linkUrl", noticeUrl);
        messagesParam.put("content", message);
        messagesParam.put("time", System.currentTimeMillis());
        messagesParam.put("topic", topic.getId());
        messagesParam.put("title", topic.getName());

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("userId", doctorId.toString());
        paramMap.put("info", JsonMapper.toJson(messagesParam));
        RestHttpUtil.post().url(NOTICE_SINGLE_URL).paramMap(paramMap).executeData();
    }

    /**
     * 个人动态推送(支持批量消息)
     * IMnoticeUtil.singleDynamics()
     *
     * @param doctor
     * @param dynamic
     *
     * @return
     *
     * @throws Exception
     * @Date 2016年1月16日
     * @since 2.0.0
     */
    public static void singleDynamics(Long doctorId, String dynamic) throws Exception {

        // 医生id校验
        if (null == doctorId || StringUtils.isEmpty(dynamic)) {
            LOGGER.error("个人动态-单人-推送参数异常 请求参数：doctorId = {}, dynamic = {}", doctorId, dynamic);
            return;
        }
        String dynamicUrl = AppJumpProtocol.DR_DYNAMIC.parse();
        String noticeImg = ConfigUtil.getString("img.notice.dynamic");

        Map<String, Object> messagesParam = new HashMap<>();
        messagesParam.put("headImg", ImageUrlUtil.genNoticePath(noticeImg));
        messagesParam.put("linkUrl", dynamicUrl);
        messagesParam.put("content", dynamic);
        messagesParam.put("time", System.currentTimeMillis());

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("userId", doctorId.toString());
        paramMap.put("info", JsonMapper.toJson(messagesParam));
        RestHttpUtil.post().url(DYNAMIC_SINGLE_URL).paramMap(paramMap).executeData();
    }

    public static String getTargetUrl(NoticeTopic topic) {
        try {
            String paramStr = URLEncoder.encode(",[{\"topic\":" + topic.getId() + "}]", "UTF-8");
            return UrlUtil.urlEncode(ConfigUtil.getString("url.notice.notice") + "?title=" + topic.getName() + "#postList") + paramStr;
        } catch (UnsupportedEncodingException e) {
            return ConfigUtil.getString("url.notice.notice") + "?title=" + topic.getName() + "#postList,[{\"topic\":" + topic.getId() + "}]";
        }
    }

}
